package org.example.utils;


import org.apache.commons.lang.StringUtils;
import org.example.entity.SysUserEntity;
import org.example.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 认证公共类,获取当前用户等信息
 *
 * @author lyf
 */
@Component
public class SecurityUtils {

    @Autowired
    private SysUserService userService ;

    public SysUserEntity getCurrentUser() {
        //获取请求token
        HttpServletRequest hsr = HttpContextUtils.getHttpServletRequest();

        String token = HttpContextUtils.getRequestToken(hsr, "token");
        String appId = HttpContextUtils.getRequestToken(hsr, "appId");
        String selectTokenMode = HttpContextUtils.getRequestToken(hsr, "select-tenant-mode");
        
        SysUserEntity ret = null;
        if (StringUtils.isNotBlank(token)) {
            ret = new SysUserEntity();
            ret.setId(1L);
//            ret = userService.getById(1);
            if(ret != null && StringUtils.isNotBlank(selectTokenMode)) {
                ret.setSelectTenantMode(selectTokenMode);
            }
        }
        return ret;
    }


    /**
     * static 方法请求 当前用户
     *
     * @return
     */
    public static SysUserEntity getUser() {

        SecurityUtils securityUtils = SpringContextUtils.getBean(SecurityUtils.class);
        if (securityUtils != null) {
            return securityUtils.getCurrentUser();
        }

        return null;

    }


}
